package service.impl;

import dao.IManagerDao;
import model.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.IManagerService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @aythor:侯仪澳
 * @data:2022/7/21
 */
@Service
public class ManagerService implements IManagerService {
    @Autowired
    private IManagerDao managerDao;

    //查询账户密码
    @Override
    public Manager adminLoginCheck(Manager manager) {
        if (manager.getmName() == null || manager.getmName().isEmpty()) return null;
        if (manager.getmPass() == null || manager.getmPass().isEmpty()) return null;
        manager.setmStatus(1);
        Map<String, Object> map = new HashMap<>();
        map.put("mana", manager);
        List<Manager> list = managerDao.queryAllAdmin(map);
//        List<Manager> list = managerDao.queryAllAdmin(manager);
        if (list == null || list.size() ==0){
            return null;
        }else {
            return list.get(0);
        }
    }

    //超级管理员查询所有管理员用户 分页
    @Override
    public Map<String,Object> queryAllAdmin(Manager manager, Integer pageNo) {
        if (pageNo == null || pageNo < 1){
            pageNo = 1;
        }
        //每一页行数
        int rowCount = 8;
        //计算数据库分页行号
        int limitFirst = (pageNo-1)*rowCount;
        Map<String, Object> map = new HashMap<>();
        map.put("mana", manager);
        map.put("rowCount", rowCount);
        map.put("limitFirst", limitFirst);
        List<Manager> list = managerDao.queryAllAdmin(map);
        //计算总页数
        List<Manager> list1 = managerDao.queryAdmin(manager);//获得所有的数据总数
        int totalPage = (int)(list1.size() / rowCount);
        if (list1.size() % rowCount != 0 ) {
            totalPage += 1;
        }
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("list", list);
        resMap.put("pageNo", pageNo);
        resMap.put("totalPage", totalPage);
        return resMap;
    }

    //添加普通管理员
    @Override
    public String insertOneManager(Manager manager) {
        int count = managerDao.insertOneManager(manager);
        if (count == 1){
            return "1";
        }else {
            return "0";
        }
    }

    //修改普通管理员的权限
    @Override
    public String updateOneManager(Manager manager) {
        int count = managerDao.updateOneManager(manager);
        if (count == 1){
            return "1";
        }else {
            return "0";
        }
    }
}
